package com.exam.api.modules.order.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.exam.api.common.exception.GlobalException;
import com.exam.api.modules.classInfo.domain.TblClassNumberInfo;
import com.exam.api.modules.classInfo.service.TblClassNumberInfoService;
import com.exam.api.modules.goods.domain.TblProductSkuInfo;
import com.exam.api.modules.goods.service.TblProductSkuInfoService;
import com.exam.api.modules.layout.service.TblSeatLayoutService;
import com.exam.api.modules.layout.service.TblSeatLayoutStuRefService;
import com.exam.api.modules.order.domain.TblOrder;
import com.exam.api.modules.order.mapper.TblOrderMapper;
import com.exam.api.modules.order.service.TblOrderService;
import com.exam.api.modules.order.vo.OrderVO;
import com.exam.api.modules.student.domain.TblStudentClassNumberRef;
import com.exam.api.modules.student.service.TblStudentClassNumberRefService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 订单逻辑实现类
 *
 * @author lingma
 */
@Service
public class TblOrderServiceImpl extends ServiceImpl<TblOrderMapper, TblOrder> implements TblOrderService {

    @Resource
    private TblClassNumberInfoService classNumberInfoService;
    @Resource
    private TblProductSkuInfoService productSkuInfoService;
    @Resource
    private TblSeatLayoutStuRefService tblSeatLayoutStuRefService;
    @Resource
    private TblSeatLayoutService tblSeatLayoutService;
    @Resource
    private TblStudentClassNumberRefService studentClassNumberRefService;



    @Override
    public List<OrderVO> studentList(Long classNumberId) {
        TblClassNumberInfo classNumberInfo = classNumberInfoService.getById(classNumberId);
        if(Objects.isNull(classNumberInfo.getSkuId())){
            throw new GlobalException("请先设置班次的商品sku");
        }
        Optional<TblProductSkuInfo> productSkuInfoOpt = productSkuInfoService
                .lambdaQuery()
                .eq(TblProductSkuInfo::getSkuId, classNumberInfo.getSkuId())
                .oneOpt();
        if(productSkuInfoOpt.isEmpty()){
            throw new GlobalException("商品sku不存在");
        }
        TblProductSkuInfo productSkuInfo = productSkuInfoOpt.get();
        List<OrderVO> orderVOS = baseMapper.selectStudentList(productSkuInfo.getSkuId(), productSkuInfo.getItemId(),classNumberId);
        return orderVOS;
    }

    @Override
    public void allocation(Long classNumberId, List<Long> userIds) {
        List<TblStudentClassNumberRef> refList = new ArrayList<>();
        userIds.forEach(userId -> {
            Optional<TblStudentClassNumberRef> opt = studentClassNumberRefService
                    .lambdaQuery()
                    .eq(TblStudentClassNumberRef::getStuId, userId)
                    .eq(TblStudentClassNumberRef::getClassNumberId, classNumberId)
                    .oneOpt();
            if(opt.isEmpty()){
                TblStudentClassNumberRef ref = new TblStudentClassNumberRef();
                ref.setStuId(userId);
                ref.setClassNumberId(classNumberId);
                refList.add(ref);
            }
        });

        if(CollUtil.isNotEmpty(refList)){
            studentClassNumberRefService.saveBatch(refList);
        }
    }
}
